﻿using ServiceCore.DbContext;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Model;

namespace ServiceCore.Service
{
    /// <summary>
    /// 阈值服务
    /// </summary>
    public partial class ThresholdService : SqlsugarServices<ThresholdInfo>
    {
        /// <summary>
        /// 分页 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<ThresholdInfo, List<ThresholdInfo>> Threshold_List(BaseParam<ThresholdInfo, List<ThresholdInfo>> arg)
        {
            arg.Data = this.Context.Queryable<ThresholdInfo>()
                .OrderBy(x => x.CreateTime)
                .ToList();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<ThresholdInfo, ThresholdInfo> Threshold_Exists(BaseParam<ThresholdInfo, ThresholdInfo> arg, bool? isAdd = null)
        {
            arg.Data = this.Context.Queryable<ThresholdInfo>()
                .WhereIF(arg.Id > 0, x => x.Id == arg.Id)
                .WhereIF(arg.Param != null && string.IsNullOrEmpty(arg.Param.Name) == false, x => x.Name == arg.Param.Name)
                .WhereIF(isAdd.HasValue && isAdd == false, x => x.Id != arg.Param.Id)
                .First();

            return arg;
        }

        /// <summary>
        /// 获取阈值数量
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<ThresholdInfo, int> Threshold_Count(BaseParam<ThresholdInfo, int> arg)
        {
            arg.Data = this.Context.Queryable<ThresholdInfo>().Count();
            arg.Data++;

            return arg;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<ThresholdInfo, long> Threshold_Add(BaseParam<ThresholdInfo, long> arg)
        {
            arg = Threshold_Add_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = InsertReturnBigIdentity(arg.Param);
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<ThresholdInfo, long> Threshold_Edit(BaseParam<ThresholdInfo, long> arg)
        {
            arg = Threshold_Edit_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = this.Context.Updateable<ThresholdInfo>()
                .SetColumns(x => new ThresholdInfo() 
                { 
                    Name = arg.Param.Name, 
                    ThresholdType = arg.Param.ThresholdType, 
                    StartValue = arg.Param.StartValue, 
                    EndValue = arg.Param.EndValue 
                })
                .Where(x => x.Id == arg.Param.Id)
                .ExecuteCommand();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<ThresholdInfo, long> Threshold_Delete(BaseParam<ThresholdInfo, long> arg)
        {
            arg = Threshold_Del_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = this.Context.Deleteable<ThresholdInfo>().Where(x => x.Id == arg.Id).ExecuteCommand();
            arg.Param = null;

            return arg;
        }
    }
}
